/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.garcomeletronico.dao.implementacao;

import br.com.garcomeletronico.bean.Cardapio;
import br.com.garcomeletronico.bean.Prato;
import br.com.garcomeletronico.dao.IDAOCardapio;
import java.sql.SQLException;
import java.util.List;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;


/**
 *
 * @author Rivi
 */
public class DAOCardapio extends DAOGenerico implements IDAOCardapio {

    @Override
    public void inserir(Cardapio pojo) throws SQLException {
        try {
            String sql = "insert into Cardapio (nome) "
                    + " values (?)";
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setString(1, pojo.getNome());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            //throw new SQLException(e.getMessage());
        }
    }

    @Override
    public void alterar(Cardapio pojo) throws SQLException {
        try {
            String sql = ("update Cardapio set nome= ?"
                    + " where codigo= ? ");
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setString(1, pojo.getNome());
            stmt.setInt(2, pojo.getCodigo());
            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

    }

    @Override
    public void excluir(Cardapio pojo) throws SQLException {
        try {
            String sql = "delete from Cardapio where codigo= ?";
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setInt(1, pojo.getCodigo());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }
    }

    @Override
    public List<Cardapio> listarTodos() throws SQLException {
        String sql = "select * from Cardapio";
        List<Cardapio> minhaLista = null;
        PreparedStatement stmt;
        ResultSet rs;

        try {
            stmt = this.getConexao().prepareStatement(sql);
            rs = stmt.executeQuery();

            while (rs.next()) {
                if (minhaLista == null) {
                    minhaLista = new ArrayList<>();
                }

                Cardapio c = new Cardapio();
                c.setCodigo(rs.getInt("codigo"));
                c.setNome(rs.getString("nome"));
                minhaLista.add(c);
            }
            rs.close();
            stmt.close();

        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

        return minhaLista;
    }

@Override
    public List<Cardapio> listarPorNome(Cardapio pojo) throws SQLException {
        String sql = "select * from Cardapio where nome like '%" + pojo.getNome() + "%'";
        List<Cardapio> minhaLista = null;
        PreparedStatement stmt;
        ResultSet rs;

        try {
            stmt = this.getConexao().prepareStatement(sql);
            rs = stmt.executeQuery();

            while (rs.next()) {
                if (minhaLista == null) {
                    minhaLista = new ArrayList<>();
                }

                Cardapio d1 = new Cardapio();
                d1.setCodigo(rs.getInt("codigo"));
                d1.setNome(rs.getString("nome"));
                minhaLista.add(d1);
            }
            rs.close();
            stmt.close();

        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

        return minhaLista;
    }

    @Override
    public Cardapio listarPorId(Cardapio pojo) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
